package com.sad.we11cis;

import java.util.ArrayList;
import java.util.Collections;

import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class ProductSearch extends Activity implements OnClickListener {

	private ListView productListView = null;
	private ListViewAdapter prodLvAdapter = null;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_product_search);
		
		Button searchButton = (Button)findViewById(R.id.btnSearchProd);
		searchButton.setOnClickListener(this);
		
		productListView = (ListView) findViewById(R.id.srcListView);
        
		prodLvAdapter = new ListViewAdapter(this);
        productListView.setAdapter(prodLvAdapter);
        
        prodLvAdapter.addItem("Product Name_1", "Product Description Here");
        
        prodLvAdapter.addItem("Product Name_2", "Product Description Here");
        
        prodLvAdapter.addItem("Product Name_3", "Product Description Here");
        
        prodLvAdapter.addItem("Product Name_4", "Product Description Here");
        
        productListView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				// TODO Auto-generated method stub
				ListData mData = prodLvAdapter.mListData.get(arg2);
				Intent intent = new Intent(ProductSearch.this, ReviewList.class);
				intent.putExtra("ProductName", mData.Title);
				startActivity(intent);
			}
		});
	}
	
	private class ViewHolder{
		
		public TextView mText;
		
		public TextView mDate;
	}
	
	private class ListViewAdapter extends BaseAdapter{

		private Context context = null;
		private ArrayList<ListData> mListData = new ArrayList<ListData>();
		
		public ListViewAdapter(Context context){
			super();
			this.context= context;
		}
		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return mListData.size();
		}

		@Override
		public Object getItem(int position) {
			// TODO Auto-generated method stub
			return mListData.get(position);
		}

		@Override
		public long getItemId(int position) {
			// TODO Auto-generated method stub
			return position;
		}
		
		public void addItem(String mTitle, String mDate){
		    ListData addInfo = null;
		    addInfo = new ListData();
		    addInfo.Title = mTitle;
		    addInfo.Date = mDate;
		             
		    mListData.add(addInfo);
		}
		 
		public void remove(int position){
		    mListData.remove(position);
		    dataChange();
		}
		 
		public void sort(){
		    Collections.sort(mListData, ListData.ALPHA_CO_COMPARATOR);
		    dataChange();
		}
		 
		public void dataChange(){
			prodLvAdapter.notifyDataSetChanged();
		}
		
		@Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder holder;
            if (convertView == null) {
                holder = new ViewHolder();
                
                LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                convertView = inflater.inflate(R.layout.listview_item, null);
                
                holder.mText = (TextView) convertView.findViewById(R.id.productName);
                holder.mDate = (TextView) convertView.findViewById(R.id.productDesc);
                
                convertView.setTag(holder);
            }else{
                holder = (ViewHolder) convertView.getTag();
            }
            
            ListData Data = mListData.get(position);
            
            holder.mText.setText(Data.Title);
            holder.mDate.setText(Data.Date);
            
            return convertView;
        }
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.product_search, menu);
		return true;
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		
		EditText productName = (EditText)findViewById(R.id.srcProductName);
		
		switch(v.getId()){
		
		case R.id.btnSearchProd:
			
			break;
		}
	}

}
